/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.Instance.L1TowerInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_Message_YN;
import com.lineage.server.packetserver.S_ServerMessage;

/**
 * Class <code>NaturesMiracle</code> 精灵魔法:生命呼唤 (50/0/1).
 * 将已死亡的对象复活，并且体力完全恢复，但恢复后魔力为零。（未开放）
 * 
 * @author jrwz
 * @version 2012-7-24下午04:22:05
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class NaturesMiracle extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new NaturesMiracle();
    }

    /** 精灵魔法:生命呼唤 (50/0/1). */
    private NaturesMiracle() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        L1PcInstance player = null;
        if (user instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) user;
            player = pc;
        }
        if (cha instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) cha;
            if (player.getId() != pc.getId()) {
                if (L1World.getInstance().getVisiblePlayer(pc, 0).size() > 0) {
                    for (final L1PcInstance visiblePc : L1World.getInstance()
                            .getVisiblePlayer(pc, 0)) {
                        if (!visiblePc.isDead()) {
                            // 复活失败，因为这个位置已被占据。
                            player.sendPackets(new S_ServerMessage(592));
                            return;
                        }
                    }
                }
                if ((pc.getCurrentHp() == 0) && pc.isDead()) {
                    pc.setTempID(player.getId());
                    pc.sendPackets(new S_Message_YN(322, "")); // 是否要复活？(Y/N)
                }
            }
        } else if (cha instanceof L1NpcInstance) {
            if (!(cha instanceof L1TowerInstance)) {
                final L1NpcInstance npc = (L1NpcInstance) cha;
                if (npc.getNpcTemplate().isCantResurrect()
                        && !(npc instanceof L1PetInstance)) {
                    return;
                }
                if ((npc instanceof L1PetInstance)
                        && (L1World.getInstance().getVisiblePlayer(npc, 0)
                                .size() > 0)) {
                    for (final L1PcInstance visiblePc : L1World.getInstance()
                            .getVisiblePlayer(npc, 0)) {
                        if (!visiblePc.isDead()) {
                            // 复活失败，因为这个位置已被占据。
                            player.sendPackets(new S_ServerMessage(592));
                            return;
                        }
                    }
                }
                if ((npc.getCurrentHp() == 0) && npc.isDead()) {
                    npc.resurrect(cha.getMaxHp());
                    npc.resurrect(cha.getMaxMp() / 100);
                    npc.setResurrect(true);
                    if ((npc instanceof L1PetInstance)) {
                        final L1PetInstance pet = (L1PetInstance) npc;
                        pet.startFoodTimer(pet); // 开始饱食度计时
                        pet.startHpRegeneration(); // 开始回血回魔
                        pet.startMpRegeneration();
                    }
                }
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        // TODO Auto-generated method stub
    }
}
